package com.earnings.okhttputils.utils.car.ui;

import android.content.DialogInterface;
import android.support.annotation.IdRes;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.earnings.R;
import com.earnings.okhttputils.utils.car.utils.StringUtils;
import com.earnings.okhttputils.utils.ui.BaseFragment;

/**
 * 全款
 * Created by Administrator on 2017/6/20 0020.
 */

public class TotalFragment extends BaseFragment implements RadioGroup.OnCheckedChangeListener, DialogInterface.OnCancelListener {
    private String title, price, name;
    private TextView price_tv, nakedcar_tv, must_tv, name_tv, nakedcar_tv2, purchase_tv2, clssj_tv,
            qcqdx_tv, zrrsx_tv, sanfangtv_tv, sbjmx_tv, wgzrx_tv, carbody_tv, boli_tv, must_tv2,
            insurance_tv, business_tv;
    private EditText sanfanged_ed, carbody_ed, boli_ed;
    private ImageView img_view1, img_view2, img_view3, img_view4, img_view5, img_view6, img_view7, img_view8, img_view9;
    private ImageView imageViews[];
    private RadioGroup meal_group;
    private double iprice;
    private SanFangXianDialog mSanFangXianDialog;
    private CarBodyDialog mCarBodyDialog;
    private BoliDialog mBoliDialog;

    @Override
    protected void init() {
        price_tv = (TextView) mView.findViewById(R.id.price_tv);
        nakedcar_tv = (TextView) mView.findViewById(R.id.nakedcar_tv);
        nakedcar_tv2 = (TextView) mView.findViewById(R.id.nakedcar_tv2);
        zrrsx_tv = (TextView) mView.findViewById(R.id.zrrsx_tv);
        purchase_tv2 = (TextView) mView.findViewById(R.id.purchase_tv2);
        boli_tv = (TextView) mView.findViewById(R.id.boli_tv);
        carbody_tv = (TextView) mView.findViewById(R.id.carbody_tv);
        insurance_tv = (TextView) mView.findViewById(R.id.insurance_tv);
        business_tv = (TextView) mView.findViewById(R.id.business_tv);
        qcqdx_tv = (TextView) mView.findViewById(R.id.qcqdx_tv);
        must_tv2 = (TextView) mView.findViewById(R.id.must_tv2);
        clssj_tv = (TextView) mView.findViewById(R.id.clssj_tv);
        must_tv = (TextView) mView.findViewById(R.id.must_tv);
        sanfangtv_tv = (TextView) mView.findViewById(R.id.sanfangtv_tv);
        wgzrx_tv = (TextView) mView.findViewById(R.id.wgzrx_tv);
        sbjmx_tv = (TextView) mView.findViewById(R.id.sbjmx_tv);
        sanfanged_ed = (EditText) mView.findViewById(R.id.sanfanged_ed);
        boli_ed = (EditText) mView.findViewById(R.id.boli_ed);
        carbody_ed = (EditText) mView.findViewById(R.id.carbody_ed);
        name_tv = (TextView) mView.findViewById(R.id.name_tv);
        meal_group = (RadioGroup) mView.findViewById(R.id.meal_group);

        img_view1 = (ImageView) mView.findViewById(R.id.img_view1);
        img_view2 = (ImageView) mView.findViewById(R.id.img_view2);
        img_view3 = (ImageView) mView.findViewById(R.id.img_view3);
        img_view4 = (ImageView) mView.findViewById(R.id.img_view4);
        img_view5 = (ImageView) mView.findViewById(R.id.img_view5);
        img_view6 = (ImageView) mView.findViewById(R.id.img_view6);
        img_view7 = (ImageView) mView.findViewById(R.id.img_view7);
        img_view8 = (ImageView) mView.findViewById(R.id.img_view8);
        img_view9 = (ImageView) mView.findViewById(R.id.img_view9);
        imageViews = new ImageView[]{img_view1, img_view2, img_view3, img_view4, img_view5, img_view6, img_view7, img_view8, img_view9};

        mView.findViewById(R.id.sanfang_view).setOnClickListener(this);
        mView.findViewById(R.id.chexian_view).setOnClickListener(this);
        mView.findViewById(R.id.boli_view).setOnClickListener(this);
        meal_group.setOnCheckedChangeListener(this);
        mSanFangXianDialog = new SanFangXianDialog(mContext);
        mCarBodyDialog = new CarBodyDialog(mContext);
        mBoliDialog = new BoliDialog(mContext);
        mSanFangXianDialog.setOnCancelListener(this);
        mCarBodyDialog.setOnCancelListener(this);
        mBoliDialog.setOnCancelListener(this);
        sanfanged_ed.setHint("保费5万");
        carbody_ed.setHint("保费2000");
        boli_ed.setHint("国产玻璃");

    }

    @Override
    public void onStart() {
        super.onStart();
        nakedcar_tv.setText(price);
        name_tv.setText(name);
        nakedcar_tv2.setText(price);
        purchase_tv2.setText(sgzs);
        clssj_tv.setText(szlssx);
        qcqdx_tv.setText(sqcqdx);
        zrrsx_tv.setText(szrssx);
        sbjmx_tv.setText(sbjmx);
        wgzrx_tv.setText(swgzrx);
        boli_tv.setText(sboli);
        must_tv2.setText(smust2);
        must_tv.setText(smust2);
        insurance_tv.setText(sinsurance);
        business_tv.setText(sinsurance);
        price_tv.setText(stotal);

    }

    @Override
    protected int onCreateLayout() {
        return R.layout.fragment_total;
    }

    @Override
    public String getTitle() {
        return title;
    }

    @Override
    public void setTitle(String title) {
        this.title = title;
    }

    public String getPrice() {
        return price;
    }

    private double gzs, zlssx, qcqdx, zrssx, bjmx, boli, must2, insurance, total;
    private String sgzs, szlssx, sqcqdx, szrssx, sbjmx, swgzrx, sboli, smust2, sinsurance, stotal;
    private int index, index2, index3;

    public void setPrice(String price, String name) {
        this.name = name;
        iprice = Double.parseDouble(price) * 10000;
        //裸车价格
        this.price = StringUtils.doublePrice2FromatPriceStr(iprice);
        //购置税(购车款/(1+17%)×购置税率(10%))
        gzs = (iprice / 1.17) * 0.1;
        gzs = Math.floor(gzs);
        sgzs = StringUtils.doublePrice2FromatPriceStr(gzs);
        //车辆损失险  基础保费(450)+裸车价格×1.0880%
        zlssx = 450 + iprice * 0.01088;
        zlssx = Math.floor(zlssx);
        szlssx = StringUtils.doublePrice2FromatPriceStr(zlssx);
        //全车盗抢险  基础保费(450)+裸车价格×费率(0.0032)
        qcqdx = 450 + iprice * 0.0032;
        sqcqdx = StringUtils.doublePrice2FromatPriceStr(qcqdx);
        //玻璃单独破碎险 进口新车购置价×0.25%，国产新车购置价×0.15%
        boli = iprice * 0.0015;
        sboli = StringUtils.doublePrice2FromatPriceStr(boli);
        //自燃损失险  新车购置价×0.15%
        zrssx = iprice * 0.0015;
        szrssx = StringUtils.doublePrice2FromatPriceStr(zrssx);
        //不计免赔特约险 (车辆损失险+第三者责任险)×20%
        bjmx = (zlssx + 516) * 0.2;
        sbjmx = StringUtils.doublePrice2FromatPriceStr(bjmx);
        //无过责任险  第三者责任险保险费×20%
        wgzrx = 516 * 0.2;
        swgzrx = StringUtils.doublePrice2FromatPriceStr(wgzrx);
        //车上人员责任险  每人保费50元(暂时先算一个)

        //    首期付款总额=首付款+必要花费+商业保险
        // 不好算   每期还款额＝贷款本金／还款期数＋（本金－已归还本金累计）×利率（月利率）

        //必要花费
        must2 = 500 + 300 + 950 + gzs;
        smust2 = StringUtils.doublePrice2FromatPriceStr(must2);
        //商业保险
        insurance = zlssx + qcqdx + boli + zrssx + bjmx + wgzrx + 50 + 400;
        sinsurance = StringUtils.doublePrice2FromatPriceStr(insurance);
        //全款
        total = insurance + must2 + iprice;
        stotal = StringUtils.doublePrice2FromatPriceStr(total);

    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.sanfang_view) mSanFangXianDialog.show();
        if (v.getId() == R.id.chexian_view) mCarBodyDialog.show();
        if (v.getId() == R.id.boli_view) mBoliDialog.show();
    }

    @Override
    public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
        for (int i = 0; i < group.getChildCount(); i++) {
            RadioButton radiobutton = (RadioButton) group.getChildAt(i);
            radiobutton.setBackgroundResource(R.drawable.meal_bg);
        }
        RadioButton radiobutton = (RadioButton) group.findViewById(checkedId);
        radiobutton.setBackgroundResource(R.drawable.meal_bg2);
        //全险
        if (checkedId == R.id.quanxian) {
            tag = 0;
            for (int i = 0; i < imageViews.length; i++) {
                imageViews[i].setImageResource(R.mipmap.cloose_hit);
            }
            zongjia();
        }
        //经济险
        if (checkedId == R.id.jingjixian) {
            tag = 1;
            for (int i = 0; i < imageViews.length; i++) {
                imageViews[i].setImageResource(R.mipmap.cloose_hit);
            }
            img_view3.setImageResource(R.mipmap.choose);
            img_view4.setImageResource(R.mipmap.choose);
            img_view5.setImageResource(R.mipmap.choose);
            img_view9.setImageResource(R.mipmap.choose);
            jingjixian();
        }

        //基本险
        if (checkedId == R.id.jibenxian) {
            tag = 2;
            for (int i = 0; i < imageViews.length; i++) {
                imageViews[i].setImageResource(R.mipmap.cloose_hit);
            }
            img_view4.setImageResource(R.mipmap.choose);
            img_view5.setImageResource(R.mipmap.choose);
            img_view6.setImageResource(R.mipmap.choose);
            img_view7.setImageResource(R.mipmap.choose);
            img_view9.setImageResource(R.mipmap.choose);
            jibenxian();
        }

    }

    //基本险
    private void jibenxian() {
        //商业险
        insurance = 50 + number + qcqdx + zlssx;
        sinsurance = StringUtils.doublePrice2FromatPriceStr(insurance);
        insurance_tv.setText(sinsurance);
        business_tv.setText(sinsurance);
        //总价
        total = insurance + must2 + iprice;
        stotal = StringUtils.doublePrice2FromatPriceStr(total);
        price_tv.setText(stotal);
    }

    //经济险
    private void jingjixian() {
        //商业险
        insurance = number + 50 + zlssx + bjmx + wgzrx;
        sinsurance = StringUtils.doublePrice2FromatPriceStr(insurance);
        insurance_tv.setText(sinsurance);
        business_tv.setText(sinsurance);
        //总价
        total = insurance + must2 + iprice;
        stotal = StringUtils.doublePrice2FromatPriceStr(total);
        price_tv.setText(stotal);
    }

    private double number = 516, wgzrx, number2 = 400, number3;
    private int tag;

    @Override
    public void onCancel(DialogInterface dialog) {
        if (dialog instanceof SanFangXianDialog) {
            //第三方责任险
            index = mSanFangXianDialog.index;
            String text = null;
            switch (index) {
                case 0:
                    number = 516;
                    break;
                case 1:
                    number = 746;
                    break;
                case 2:
                    number = 924;
                    break;
                case 3:
                    number = 1252;
                    break;
                case 4:
                    number = 1630;
                    break;
            }
            text = StringUtils.doublePrice2FromatPriceStr(number);
            sanfangtv_tv.setText(text);
            sanfanged_ed.setHint(mSanFangXianDialog.text);
            //不计免赔特约险 (车辆损失险+第三者责任险)×20%
            bjmx = (zlssx + number) * 0.2;
            sbjmx = StringUtils.doublePrice2FromatPriceStr(bjmx);
            sbjmx_tv.setText(sbjmx);
            //无过责任险  第三者责任险保险费×20%
            wgzrx = number * 0.2;
            swgzrx = StringUtils.doublePrice2FromatPriceStr(wgzrx);
            wgzrx_tv.setText(swgzrx);
        }
        if (dialog instanceof CarBodyDialog) {
            carbody_ed.setHint(mCarBodyDialog.text);
            index2 = mCarBodyDialog.index;
            String text = null;
            switch (index2) {
                case 0:
                    number2 = 400;
                    break;
                case 1:
                    number2 = 570;
                    break;
                case 2:
                    number2 = 760;
                    break;
                case 3:
                    number2 = 1140;
                    break;
            }
            //车身划痕险
            text = StringUtils.doublePrice2FromatPriceStr(number2);
            carbody_tv.setText(text);
        }
        if (dialog instanceof BoliDialog) {
            boli_ed.setHint(mBoliDialog.text);
            index3 = mBoliDialog.index;
            String text = null;
            switch (index3) {
                case 0:
                    number3 = iprice * 0.0015;
                    break;
                case 1:
                    number3 = iprice * 0.0025;
                    break;
            }

            //玻璃单独破碎险 进口新车购置价×0.25%，国产新车购置价×0.15%
            text = StringUtils.doublePrice2FromatPriceStr(number3);
            boli_tv.setText(text);
        }

        switch (tag) {
            case 0:
                zongjia();
                break;
            case 1:
                jingjixian();
                break;
            case 2:
                jibenxian();
                break;
        }


    }

    private void zongjia() {
        //商业保险
        insurance = zlssx + qcqdx + boli + zrssx + bjmx + wgzrx + 50 + number2;
        sinsurance = StringUtils.doublePrice2FromatPriceStr(insurance);
        insurance_tv.setText(sinsurance);
        business_tv.setText(sinsurance);
        //总价
        total = insurance + must2 + iprice;
        stotal = StringUtils.doublePrice2FromatPriceStr(total);
        price_tv.setText(stotal);

    }
}
